package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerServices;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：MJS
 * @date ：Created in 2021/6/10  9:02
 * @description：TODO
 */
@Service
@SuppressWarnings("all")
public class ConsumerServicesImpl implements ConsumerServices {
    @Autowired(required = false)
    private ConsumerMapper consumerMapper;
    //按条件模糊查询查询物资去处
    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, ConsumerVO consumerVO) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Consumer.class);
        String address = consumerVO.getAddress();
        String contact = consumerVO.getContact();
        String name = consumerVO.getName();
        Example.Criteria criteria = o.createCriteria();
        if(address!=null&&!"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if(contact!=null&&!"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        if(name!=null&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        List<Consumer> consumers = consumerMapper.selectByExample(o);
        List<ConsumerVO> consumerVos = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO1 = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO1);
            return consumerVO1;
        }).collect(Collectors.toList());
        PageInfo<Supplier> info=new PageInfo(consumers);
        return new PageVO<>(info.getTotal(),consumerVos);
    }

    //添加物资去处
    @Override
    @Transactional
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
    }
    //编辑物资去处信息    回显
    @Transactional
    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        Consumer consumer1 = consumerMapper.selectByPrimaryKey(consumer);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer1,consumerVO);
        return consumerVO;
    }
    //修改物资去处信息
    @Override
    public void update(Long id, ConsumerVO consumerVO) {
        consumerVO.setModifiedTime(new Date());
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        Example o = new Example(consumer.getClass());
        o.createCriteria().andEqualTo("id",id);
        consumerMapper.updateByExample(consumer,o);
    }
    //删除物资去处信息
    @Override
    public void delete(Long id) {
        Consumer consumer = new Consumer();
        consumer.setId(id);
        consumerMapper.delete(consumer);
    }
    //查询全部数据
    @Override
    public List<ConsumerVO> findAll() {
        Example o = new Example(Consumer.class);
        List<Consumer> consumerList = consumerMapper.selectByExample(o);
        List<ConsumerVO> consumerVoList = consumerList.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        return consumerVoList;
    }
}
